//
//  YHGoodsHelper.m
//  YHGoods
//
//  Created by yanHu on 2024/4/17.
//

#import "YHGoodsHelper.h"

@implementation YHGoodsHelper

+ (UIImage *)imageNamed:(NSString *)name 
{
    static NSBundle *resourceBundle = nil;
    if (!resourceBundle) {
        NSBundle *mainBundle = [YHGoodsHelper mainBundle];
        NSString *resourcePath = [mainBundle pathForResource:@"YHGoodsResources" ofType:@"bundle"];
        resourceBundle = [NSBundle bundleWithPath:resourcePath] ?: mainBundle;
    }
    UIImage *image = [UIImage imageNamed:name inBundle:resourceBundle compatibleWithTraitCollection:nil];
    return image;
}

@end

@implementation YHGoodsHelper (Bundle)

+ (NSBundle *)mainBundle 
{
    return [NSBundle bundleForClass:self];
}

@end

@implementation YHGoodsHelper (Window)

+ (UIWindow *)window 
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    if (window) {
        return window;
    }
    
    if (@available(iOS 13.0, *)) {
        for (UIWindowScene* windowScene in [UIApplication sharedApplication].connectedScenes) {
            if (windowScene.activationState == UISceneActivationStateForegroundActive) {
                window = windowScene.windows.firstObject;
                break;
            }
        }
    }
    
    return window;
}

@end

